package com.apex.crm.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname BaseService
 * @description TODO
 * @date 2021/4/19 20:47
 */

@SuppressWarnings("all")
public abstract class BaseService<T,ID> {
    @Autowired
    private BaseMapper<T,ID> baseMapper;

    //插入记录返回行数
    public Integer insertSelective(T entity) throws DataAccessException{
        return baseMapper.insertSelective(entity);
    }

    //添加记录返回主键
    public ID insertHasKey(T entity) throws DataAccessException{
        baseMapper.insertHasKey(entity);
        try{
            return (ID)entity.getClass().getMethod("getId").invoke(entity);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //批量添加
    public Integer insertBatch(List<T> entities) throws DataAccessException{
        return baseMapper.insertBatch(entities);
    }

    //根据ID查询详情
    public T selectByPrimaryKey(ID id) throws DataAccessException{
        return baseMapper.selectByPrimaryKey(id);
    }

//    public Map<String, Object> queryByParamsForTable(BaseQuery baseQuery){
//        Map<String,Object> result = new HashMap<String,Object>();
//        PageHelper.startPage(baseQuery.getPage(),baseQuery.getLimit());
//        PageInfo<T> pageInfo =new PageInfo<T>(selectByParams(baseQuery));
//        result.put("count",pageInfo.getTotal());
//        result.put("data",pageInfo.getList());
//        result.put("code",0);
//        result.put("msg","");
//        return result;
//    }





}
